<template>
  <div>
    <div v-if="query.serviceId === null">
      <div class="my-code">点击查看授权角色</div>
    </div>
    <div v-else>
      <!--工具栏-->
      <div class="head-container">
        <div v-if="crud.props.searchToggle">
          <!-- 搜索 -->
          <el-input
            v-model="query.label"
            clearable
            size="small"
            placeholder="输入字典标签查询"
            style="width: 200px;"
            class="filter-item"
            @keyup.enter.native="toQuery"
          />
          <rrOperation />
        </div>
      </div>
      <!--表单组件-->
      <el-dialog
        append-to-body
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible="crud.status.cu > 0"
        :title="crud.status.title"
        width="400px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="角色" prop="role.id">
            <el-select v-model="form.role.id" placeholder="请选择角色">
              <el-option
                v-for="item in roles"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        highlight-current-row
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column label="角色名称" prop="role.name" />
        <el-table-column v-permission="['admin','service:del']" label="操作" width="130px" align="center" fixed="right">
          <template slot-scope="scope">
            <ud-role
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import CRUD, { presenter, header, form } from '@crud/crud'
import pagination from '@crud/Pagination'
import rrOperation from '@crud/RR.operation'
import udRole from '@crud/UD.role'
import roleService from '@/api/system/roleService'
import role from '@/api/system/role'

const defaultForm = { id: null, role: { id: null }}
export default {
  name: 'RoleService',
  components: { pagination, rrOperation, udRole },
  cruds() {
    return [
      CRUD({
        title: '服务角色授权', url: 'rest/api/manager/rservice', query: { serviceId: null },
        crudMethod: { ...roleService },
        optShow: {
          add: true,
          edit: true,
          del: true,
          reset: false
        },
        queryOnPresenterCreated: false
      })
    ]
  },
  mixins: [
    presenter(),
    header(),
    form(function() {
      return Object.assign({ serviceId: this.serviceId }, defaultForm)
    })],
  data() {
    return {
      serviceId: null,
      roles: [],
      permission: {
        add: ['admin', 'service:add'],
        edit: ['admin', 'service:edit'],
        del: ['admin', 'service:del']
      },
      rules: {
        role: {
          id: [{ required: true, message: '请选择角色', trigger: 'changer' }]
        }
      }
    }
  },
  created() {
    role.getAll().then(res => {
      this.roles = res
    }).catch(() => {
      console.log('获取角色信息失败！')
    })
  }
}
</script>

<style scoped>

</style>
